#!/usr/bin/python

'''
Description: This script is a wrapper to the ldappm.cpp file 

Created on: Jun 27, 2011

@author: Clint P. George 
'''

import os
import glob 
import subprocess

from numpy import *



 

def build_all(source_dir, recompile=False):
    
    exit_status = 0 
    
    if recompile: 
        cmd = ['make', 'clean']
        exit_status = subprocess.Popen(cmd, cwd=source_dir).wait()

    cmd = ['make']
    exit_status += subprocess.Popen(cmd, cwd=source_dir).wait()        

    return exit_status

def run_lda_batch(source_dir, data_dir,  data_file, vocab_file, output_prefix):
    
    algorithm = 'lda'

    lda_cmd = ['./ldappm']
    
    lda_cmd.append('--algorithm')
    lda_cmd.append(algorithm)
    
    lda_cmd.append('--data')
    lda_cmd.append(os.path.join(data_dir, data_file))
    
    lda_cmd.append('--data_format')
    lda_cmd.append('ldac')
    
    lda_cmd.append('--vocab')
    lda_cmd.append(os.path.join(data_dir, vocab_file))
    
    lda_cmd.append('--topics')
    lda_cmd.append('10')
    
    lda_cmd.append('--max_iter')
    lda_cmd.append('100')
    
    lda_cmd.append('--burn_in')
    lda_cmd.append('90')
    
    lda_cmd.append('--output_prefix')
    lda_cmd.append(output_prefix)
    
    lda_cmd.append('--output_dir')
    lda_cmd.append(data_dir)
    
  
    exit_status = subprocess.Popen(lda_cmd, cwd=source_dir)
    
    return exit_status



if __name__ == '__main__':
    
    work_dir = os.getcwd()
    root_dir = '/home/clint/Dropbox/TREC/'
    batch_dir = 'batch'
    query_dir = '/home/clint/Dropbox/TREC/query/1'
    output_prefix = 'ts'
    build_files = True
    max_iter = 50
    burn_in_period = 40
    saved_beta = 'hdp-topics.dat'
    
    
    for root, dirs, files in os.walk(os.path.join(root_dir, batch_dir)):
        
        print 'reading ', root, '...' 
        
        for file in files:            
            if file == saved_beta:

                with open(os.path.join(root, file), 'r') as fbeta:
                    count = 0
                    for eachLine in fbeta:
                        count += 1
                        ll = eachLine.strip().split()                        
                        ll2 = [map(int, x) for x in ll]
                        
                        # print sum(ll2)
                        
                        if count == 1: 
                            m = array(ll2)
                        else:
                            m = vstack([m, array(ll2)])
                
                num_topics = m.shape[0]
                num_words = m.shape[1]
                
                row_sums = m.sum(axis=1)
                
                print row_sums.mean()
                
                print num_topics, num_words
                

                
            



    
#    if build_files: 
#        build_all(work_dir, False)
    

    # run_lda_batch(work_dir, data_dir, data_file, vocab_file, output_prefix)
    
    
    
    
#    
#    with open(output_prefix + '_theta_samples_mean.dat', 'r') as fbeta:
#        for line in fbeta: 
#            print line.strip()


    
    
            
            
        

    
    
    
    
    